<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/10/16
 * Time: 10:54
 */

namespace app\common\service;


use think\Db;

class Host extends BaseService
{
    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/10/24 13:55
     */
    public function modify($condition, $updateData)
    {
        return Db::name('host')->where($condition)->update($updateData);
    }
    /*
     * @name: 系统比率
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-06-04 14:00:04
     */
    public function osStatistics()
    {
        $osRelation = model('Host')->group('os')->column('count(*)', 'os');
        $return = [];
        foreach ($osRelation as $k => $v)
        {
            $child = [
                'name' => model('Host')->showOs($k),
                'value' => $v,
            ];
            $return[] = $child;
        }

        return $return;
    }

    /**
     * @name: 列表
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-24 15:48:17
     */
    public function list($data, $module, $isIncludeGoupIdNull = false)
    {
        $Host = Db::name('Host')->alias('h')
            ->join('HostModuleRelation hmr', 'hmr.host_id = h.id')
            ->join('HostLink hl', 'h.id = hl.host_id')
            ->where('hmr.'.$module, 1)->where('hl.module', $module);

        //本表查询
        isset($data['ip']) && !empty($data['ip']) ? $Host->whereLike('h.ip', '%' . $data['ip'] . '%') : '';
        isset($data['name']) && !empty($data['name']) ? $Host->whereLike('h.name', '%' . $data['name'] . '%') : '';//资产名
        isset($data['os']) && is_numeric($data['os']) ? $Host->where('h.os', '=', $data['os']) : '';//系统
        isset($data['ministry']) && is_numeric($data['ministry']) ? $Host->where('h.ministry_id', '=', $data['ministry']) : '' ;//部门id
        isset($data['manager']) && is_numeric($data['manager']) ? $Host->where('h.manager_id', '=', $data['manager']) : '';//管理员id
        isset($data['tag']) && is_numeric($data['tag']) ? $Host->where("FIND_IN_SET('" . $data['tag'] . "', tag)") : '' ;
        //根据当前用户所属资产组id查询 所属当前用户所属资产组
        if (isset($data['group_id']) && is_numeric($data['group_id'])) {
            $data['group_id'] = intval($data['group_id']);
            //只查询对应当前用户所属资产组数据
            if(!$isIncludeGoupIdNull){
                $Host->where('h.group_id', '=', $data['group_id']);
            }else{
                //查询对应当前用户所属资产组以及没有组的数据
                $Host->where('h.group_id', ['=', $data['group_id']], ['=', 0], 'or');
            }

        }

        //主机联系表
        isset($data['online']) && is_numeric($data['online']) ? $Host->where('hl.status', $data['online']) : '';
        //netfilter 信息查询
        isset($data['learn_status']) && is_numeric($data['learn_status']) ? $Host->join('HostLearnConfig hlc', 'h.id = hlc.host_id')->where('hlc.learn_status', $data['learn_status']) : '';//学习状态

        $field = 'h.id,h.name,h.alias,h.ip,h.software_id,h.system_name,h.ministry_id,h.manager_id,h.address_id,h.os,h.safe_provider_id,h.maintain_provider_id,h.develop_provider_id,h.tag
        ,hl.status as online,hl.version, h.hostkey,h.group_id';
        $Host->field($field);

        $return = $Host->order('h.id', 'desc')->paginate()->toArray();
        return $return;
    }

    /**
     * @name: 详情
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-25 11:24:18
     */
    public function info ($host_id)
    {
        $return = Db::name('Host')->alias('h')->field('h.id,h.name,h.alias,h.ip,h.dns,h.gateway,h.software_id,h.system_name,h.ministry_id,h.manager_id,h.address_id,h.os,h.safe_provider_id,h.maintain_provider_id,h.develop_provider_id,h.tag')->find($host_id);

        return $return;
    }

    /**
     * @name: 主机属性列表
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-11-15 10:51:36
     */
    public function hostList ($host_id_a, $field)
    {
        $return = Db::name('Host')->whereIn('id', $host_id_a)->orderRaw("field(id," . implode(',', $host_id_a) . ")")->column($field, 'id');

        return $return;
    }

    /**
     * @name: 查找通过id
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-11-15 16:18:50
     */
    public function findById ($host_id)
    {
        $return = Db::name('Host')->find($host_id);

        return $return;
    }

    /**
     * @name: 标签比率
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-11-20 17:07:20
     */
    public function tagStatistics ()
    {
        $HostTag = new HostTag();
        $tag_a = $HostTag->showTag([], 'name', 'id');
        $return = [];
        foreach ($tag_a as $k => $v)
        {
            $return[] = [
                'name' => $v,
                'value' => Db::name('Host')->where('FIND_IN_SET(' . "'" . $k . "'" . ',tag)')->count(),
            ];
        }

        return $return;
    }


    /**
     * @name: 存活主机
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-12-03 16:25:16
     */
    public function existNum()
    {
        $return = Db::name('Host')->count();

        return $return;
    }

    /**
     * @name 通过host_id获取ip
     * @param $host_id
     * @return mixed
     * @author wx
     * @date 2019/12/25 10:57
     */
    public function GetHostIp($host_id)
    {
        return Db::name('Host')->where('id',$host_id)->value('ip');
    }

    /**
     * @name 获取当前组的主机
     * @param $groupId
     * @author wx
     * @date 2020/10/22 15:47
     */
    public function GetGroupHost($groupId)
    {
        $groupId = [$groupId, 0, -1];
        return Db::name('host')->whereIn('group_id', $groupId)->column('id');
    }


}